﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;
using MySql.Data.Types;
using TAMS;

namespace TAMS.Admin
{
    public partial class Admin_FacMemAdd : System.Web.UI.Page
    {
        MySqlConnection conn;
        MySqlCommand sqlCommand;
        MySqlDataReader readVals;
        string strProvider = Resource1.databaseConnection;

        protected void Page_Load(object sender, EventArgs e)
        {
            ((Menu)Master.FindControl("Menu1")).Items[0].Selected = true;
            Master.FindControl("ContentPlaceholder1").FindControl("Button2").Visible = false;
            Master.FindControl("ContentPlaceholder1").FindControl("Button3").Visible = false;
            Master.FindControl("ContentPlaceholder1").FindControl("Button4").Visible = false;
        }

        protected void Add_Click(object sender, EventArgs e)
        {
            /*Get info from textboxes*/

            string id = ID.Text;
            string name = Name.Text;
            string rank = DropDownList1.SelectedValue.ToString();

            DateTime bdate = Convert.ToDateTime(BDate.Text);
            string bdateInfo = bdate.Year + "-" + bdate.Month + "-" + bdate.Day;

            DateTime edate = Convert.ToDateTime(EDate.Text);
            string edateInfo = edate.Year + "-" + edate.Month + "-" + edate.Day;

            string email = Email.Text;
            string pass = Password.Text;
            string url = ImgUrl.Text;

            
//             string housingInfo = Housing.Text;
// 
//             DateTime resSince = Convert.ToDateTime(ResSince.Text);
//             string resTime = edate.Year + "-" + edate.Month + "-" + edate.Day;

            // Connect to the Database
            conn = new MySqlConnection(strProvider);
            conn.Open();

            string comma = ", ";

            string insertStaff = "INSERT "
                                 + "INTO Staff "
                                 + "VALUES (" + id + comma + "'" + name + "'" + comma + "'" + bdateInfo + "'" + comma + "'" + edateInfo + "'" + comma + "'" + email + "'" + comma + "'" + pass + "'" + comma + "'" + url + "'" + " )";

            string insertFacMem = "INSERT "
                                 + "INTO FMember "
                                 + "VALUES ('" + rank + "'" + comma + id + ")";
// 
//             string insertFResides = "INSERT "
//                                  + "INTO FResides "
//                                  + "VALUES (" + resTime + comma + housingInfo + comma + id + ")";
// 
//             DateTime wsince = Convert.ToDateTime(Wsince.Text);
//             string wSince = wsince.Year + "-" + wsince.Month + "-" + wsince.Day;
//             
//             string inserOffice = "INSERT "
//                                  + "INTO FWorks "
//                                  + "VALUES (" + id + comma + "'" + Office.Text + "'" + comma + wSince + ")";
//  

            sqlCommand = new MySqlCommand(insertStaff, conn);

            Boolean checker = false;
            try
            {
                readVals = sqlCommand.ExecuteReader();
                readVals.Close();

                sqlCommand = new MySqlCommand(insertFacMem, conn);

                readVals = sqlCommand.ExecuteReader();
                readVals.Close();

//                 sqlCommand = new MySqlCommand(insertFResides, conn);
// 
//                 readVals = sqlCommand.ExecuteReader();
// 
//                 sqlCommand = new MySqlCommand(inserOffice, conn);
// 
//                 readVals = sqlCommand.ExecuteReader();
//                 readVals.Close();

            }
            catch (MySqlException ex)
            {

                string msg = "Insert Error:";
                msg += ex.Message;
                ErrorLiteral.Text = msg;
                ErrorLiteral.Visible = true;
                checker = true;

            }

            if (!checker)
            {
                Response.Redirect("Admin_FacMem.aspx");
            }
        }

        protected void Cancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("Admin_FacMem.aspx");
        }
    }
}